﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ESOL.Common.Entity;
using ESOL.Business.Fa;
using ESOL.Common;

namespace ESOL.WinUI.Fa
{
    public partial class frmManageAsset : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private FaAssetsEntity mSelectedAsset;
        private static List<FaAssetsEntity> mtreelist;
        #endregion

        #region Construction
        public frmManageAsset()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "FAS", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(repositoryItemGridLookUpEdit1, "FAS", true);
        }

        private string GetCondition()
        {
            string condition = "";
            if (txtAssetType.Tag != null && txtAssetType.Tag.GetType() == typeof(FaAssetsTypeEntity))
            {
                FaAssetsTypeEntity type = (FaAssetsTypeEntity)txtAssetType.Tag;
                condition += " AND (AT1.ASSET_TYPE_ID = '" + type.AssetTypeId + "' OR AT2.ASSET_TYPE_ID = '" + type.AssetTypeId + "' OR AT3.ASSET_TYPE_ID = '" + type.AssetTypeId + "' OR AT4.ASSET_TYPE_ID = '" + type.AssetTypeId + "' OR AT5.ASSET_TYPE_ID = '" + type.AssetTypeId + "') ";
            }
            if (string.IsNullOrEmpty(cmbStatus.Text) == false && cmbStatus.SelectedValue != null)
            {
                condition += " AND FA.STATUS = '" + cmbStatus.SelectedValue.ToString() + "' ";
            }
            if (string.IsNullOrEmpty(txtAssetCode.Text) == false)
            {
                condition += " AND FA.ASSET_CODE LIKE '%" + txtAssetCode.Text + "%' ";
            }
            if (string.IsNullOrEmpty(txtAssetSpec.Text) == false)
            {
                condition += " AND FA.ASSET_SPEC = '" + txtAssetSpec.Text + "' ";
            }
            if (string.IsNullOrEmpty(txtAssetName.Text) == false)
            {
                condition += " AND FA.ASSET_NAME LIKE '%" + txtAssetName.Text + "%' ";
            }
            if (string.IsNullOrEmpty(txtProductionCode.Text) == false)
            {
                condition += " AND FA.PRODUCTION_CODE LIKE '%" + txtProductionCode.Text + "%' ";
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "AT2.ASSET_TYPE_NAME, AT1.ASSET_TYPE_NAME, FA.ASSET_CODE";
            DataTable dt = FaAssetsBs.GetByCondition(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }

        private void BindTree()
        {
            treeAsset.ClearNodes();
            if (mSelectedAsset == null||string.IsNullOrEmpty(mSelectedAsset.AssetId))
            {
                return;
            }
            mtreelist = new List<FaAssetsEntity>();
            FaAssetsBs.GetAllChildAssets(mSelectedAsset,mtreelist);
            
                try
                {
                    if (mtreelist.Count > 0)
                    {
                        treeAsset.DataSource = mtreelist;
                        treeAsset.ExpandAll();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message,"error",MessageBoxButtons.OK,MessageBoxIcon.Error);
                }
            
        }

        #endregion

        private void txtAssetType_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainAssetTypeQuery frm = new frmMaintainAssetTypeQuery();
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                txtAssetType.Text = frm.SelectedAssetType.AssetTypeName;
                txtAssetType.Tag = frm.SelectedAssetType;
            }
            else
            {
                txtAssetType.Text = "";
                txtAssetType.Tag = null;
            }
        }

        protected override void OnShown(EventArgs e)
        {
            BindComboBox();
            BindDataGrid();
            BindTree();
            base.OnShown(e);
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedAsset = new FaAssetsEntity(dr);
            BindTree();
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        protected override void Click_View()
        {
            if (mSelectedAsset == null)
            {
                MessageBox.Show("请选择一个固定资产！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainAsset frm = new frmMaintainAsset();
            frm.mSelectedAsset = mSelectedAsset;
            frm.ThisActionType = ActionType.View;
            frm.ShowDialog();

        }

        protected override void Click_New()
        {
            frmMaintainAsset frm = new frmMaintainAsset();
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Delete()
        {
            if (mSelectedAsset == null)
            {
                MessageBox.Show("请选择一个固定资产！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否删除选中固定资产？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                bool successful = FaAssetsBs.Delete(mSelectedAsset);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Modify()
        {
            if (mSelectedAsset == null)
            {
                MessageBox.Show("请选择一个固定资产！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainAsset frm = new frmMaintainAsset();
            frm.ThisActionType = ActionType.Modify;
            frm.mSelectedAsset = mSelectedAsset;
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                BindDataGrid();
            }
        }


    }
}